package cn.mxz.robots.province;

import net.sf.json.JSONObject;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import cn.mxz.crawler_single.basic.Request;
import cn.mxz.rds.pojo.GaoKaoProvince;
import cn.mxz.rds.pojo.GaoKaoRank;
import cn.mxz.rds.service.GaoKaoProvinceService;
import cn.mxz.rds.service.GaoKaoRankService;
import cn.mxz.robots.basic.local.Exam;
import cn.mxz.robots.basic.local.IRank;
import cn.mxz.robots.pojo.Student;

/**
 * 重庆高考查询
 * 排名:完成
 * 去年排名:完成
 * 
 * @author cuizhennan <cuizhennan@mouge.wang>
 * @since 2015-06-23
 */
public class Chongqing extends Exam implements IRank
{

	public Chongqing(Student student)
	{
		super(student);
	}

	// public static void main(String[] args)
	// {
	// Student student = new Student();
	// student.setStudentId("15500127111237");
	// student.setStudentPwd("965998");
	// student.setType("bl");
	// Chongqing guangxi = new Chongqing(student);
	// System.out.println(guangxi.getCaptcha());
	// System.out.println("请输入验证码：");
	// @SuppressWarnings("resource")
	// String captcha = new Scanner(System.in).next();
	// System.out.println(guangxi.login(captcha));
	// }

	@Override
	public String getCaptcha()
	{
		Request captchaRequest = new Request("http://gkcj.cqksy.cn/kaptcha.jpg", mSha1);

		return curlFile(captchaRequest);
	}

	@Override
	public String login(String captcha)
	{
		Request loginRequest = new Request("http://gkcj.cqksy.cn/Login", mSha1);
		loginRequest.addParams("ksh", mStudent.getStudentId());
		loginRequest.addParams("pass", mStudent.getStudentPwd());
		loginRequest.addParams("valid", captcha);
		String loginHtml = curlPost(loginRequest);

		JSONObject resultObject = new JSONObject();

		if (loginHtml.contains("Ok"))
		{
			Request suceRequest = new Request("http://gkcj.cqksy.cn/score.jsp", mSha1);
			String suceHtml = curlPost(suceRequest);

			resultObject.put("result", LOGIN_SUCCESS);
			resultObject.put("data", loginParseHtml(suceHtml));
		}
		else if (loginHtml.contains("PassError"))
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", WRONG_USER_PASSWORD);
		}
		else if (loginHtml.contains("ValidCodeError"))
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", WRONG_CAPTCHA);
		}
		else
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", INTERNAL_SERVER_ERROR);
		}

		return resultObject.toString();
	}

	@Override
	public String loginParseHtml(String loginHtml)
	{
		Document loginDocument = Jsoup.parse(loginHtml);
		Elements trs = loginDocument.select(".tab tr");

		JSONObject data = new JSONObject();
		data.put("exam_id", mStudent.getStudentId());
		data.put("name", regexMatch(trs.get(1).text(), "\u59d3\u540d\uff1a(.*)").replace(" ", ""));
		data.put(
				"ranking",
				getRank(3 + 31, regexMatch(trs.get(6).select("td").get(1).text(), "(.*)\u5206").trim(),
						mStudent.getType()));
		data.put("province", "重庆市");

		JSONObject score = new JSONObject();
		score.put("语文", regexMatch(trs.get(2).select("td").get(1).text(), "(.*)\u5206").replace(" ", ""));
		score.put("数学", regexMatch(trs.get(3).select("td").get(1).text(), "(.*)\u5206").replace(" ", ""));
		score.put("外语", regexMatch(trs.get(4).select("td").get(1).text(), "(.*)\u5206").replace(" ", ""));
		score.put("综合", regexMatch(trs.get(5).select("td").get(1).text(), "(.*)\u5206").replace(" ", ""));
		score.put("总成绩", regexMatch(trs.get(6).select("td").get(1).text(), "(.*)\u5206").replace(" ", ""));
		data.put("score", score.toString());

		return data.toString();
	}

	@Override
	public void initRank()
	{
		// 2015年文科含加分本科一分段表
		// http://www.cqksy.cn/site/infopub/2015/2015_gk/2015_gk_yfdb/wk1.htm
		// 2015年理科含加分本科一分段
		// http://www.cqksy.cn/site/infopub/2015/2015_gk/2015_gk_yfdb/lk1.htm
		String rankHtml = curlGet(new Request("http://www.cqksy.cn/site/infopub/2015/2015_gk/2015_gk_yfdb/lk1.htm",
				mSha1));
		// String rankHtml = curlGet(new
		// Request("http://www.cqksy.cn/site/infopub/2015/2015_gk/2015_gk_yfdb/wk1.htm",
		// mSha1));

		Elements trs = Jsoup.parse(rankHtml).select("table tr");

		int trSize = trs.size() - 2;
		for (int i = 3; i < trSize; i++)
		{
			Elements tds = trs.get(i).select("td");
			Integer score = Integer.valueOf(tds.get(0).text());
			Integer counter = Integer.valueOf(tds.get(1).text());
			Integer ranking = Integer.valueOf(tds.get(2).text());

			GaoKaoProvinceService provinceService = new GaoKaoProvinceService();
			GaoKaoProvince province = provinceService.get(3 + 31);
			GaoKaoRankService rankService = new GaoKaoRankService();

			GaoKaoRank rank = new GaoKaoRank();
			rank.setScore(score);
			rank.setCount(counter);
			rank.setType("bw");
			rank.setRanking(ranking);
			rank.setGaoKaoProvince(province);
			rankService.save(rank);

			System.out.println(score + "--" + counter + "--" + ranking);
		}

	}

	@Override
	public void lastYearRank()
	{
		// 理 http://www.cqzk.com.cn/www2014/lk1.htm
		// 文 http://www.cqzk.com.cn/www2014/wk1.htm
		Request request = new Request("http://www.cqzk.com.cn/www2014/lk1.htm", mSha1);
		Document requestDoc = Jsoup.parse(curlGet(request));

		Elements trs = requestDoc.select("table").get(0).select("tr");

		for (int i = 3; i < trs.size() - 1; i++)
		{
			Integer scores = Integer.parseInt(trs.get(i).select("td").get(1).text());
			Integer counters = Integer.parseInt(trs.get(i).select("td").get(2).text());
			Integer rankings = Integer.parseInt(trs.get(i).select("td").get(3).text());
			System.out.println(scores + ":" + counters + ":" + rankings);

			GaoKaoProvinceService provinceService = new GaoKaoProvinceService();
			GaoKaoProvince province = provinceService.get(3);
			GaoKaoRankService rankService = new GaoKaoRankService();
			GaoKaoRank rank = new GaoKaoRank();
			rank.setScore(scores);
			rank.setCount(counters);
			rank.setType(LI_KE);
			rank.setRanking(rankings);
			rank.setGaoKaoProvince(province);
			rankService.save(rank);
		}
		// public static void main(String[] args)
		// {
		// new Chongqing(new Student()).initRank();
		// }

	}
}
